// @ts-nocheck

import axios from 'axios';
// var server_url = "http://localhost";
// var server_url = "http://121.40.119.36";
// var server_url = "http://152.136.203.122";
var server_url = "https://tie.cyue.net";
const logResult = false;


/**
 * @param date new Date()
 * @returns {string} “yyyy-MM-dd HH:mm:ss”
 * */
export function formatDate(date)
{
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    var result = Y + M + D +" "+h + m + s;
    console.log("formatDate: " + result);
    return result;
}


/**
 * @param {string} interfacePath 接口路径
 * @returns {string}
 */
function getInterfaceURL(interfacePath)
{
    return server_url + interfacePath;
}


/**
 * @param {string} url 
 * @param {object | null | undefined} data 
 * @returns
 */
async function getMethod(url)
{
    var result = null;
    await axios({
        method: 'get',
        url: url
    }).then(function(response) {
        result = response.data;
        if (logResult)
		    console.log(result);
    });
    return result;
}


/**
 * @param {string} url 
 * @param {object | null | undefined} data 
 * @returns
 */
async function getMethodForm(url, params)
{
    var result = null;
    await axios({
        method: 'get',
        url: url,
        params: params
    }).then(function(response) {
        result = response.data;
        if (logResult)
		    console.log(result);
    });
    return result;
}


/**
 * @param {string} url 
 * @param {object | null | undefined} data 
 * @returns
 */
async function postMethod(url, data)
{
    var result = null;
    await axios({
        method: 'post',
        url: url,
        data: data
    }).then(function(response) {
        result = response.data;
        if (logResult)
		    console.log(result);
    });
    return result;
}


/**
 * @param {string} url
 * @param {object | null | undefined} params
 * @returns
 */
async function postMethodForm(url, params)
{
    var result = null;
    await axios({
        method: 'post',
        url: url,
        params: params,
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
		}
    }).then(function(response) {
        result = response.data;
        if (logResult)
		    console.log(result);
    });
    return result;
}


/**
 * @param {string} username 用户名
 * @param {string} password 密码
 * @returns {Promise<string | undefined>} result 为 token
*/
export async function loginAdminer(username, password)
{
    if (username == null || username.length == 0)
    {
        alert("用户名不能为空");
        return;
    }
    if (password == null || password.length == 0)
    {
        alert("密码不能为空");
        return;
    }

    var token = "";

    var login_url = getInterfaceURL("/adminer/login");
    var data = {
        username: username,
        password: password
    }
    await postMethod(login_url, data).then(function(result) {
        alert(result.message);
        if (result.type ===  "Success")
        {
            axios.defaults.headers.common['access_token'] = result.data;
            token = result.data;
        }
    });
    return token;
}



/**
 * 需要先登录
 * @returns {Promise<object | undefined>} result 为 用户json对象
 */
export async function getAdminerSelf()
{
    var adminer;
    var url = getInterfaceURL("/adminer/getAdminerSelf");
    await getMethod(url).then(function(result) {
        if (result.type ===  "Success")
        {
            adminer = result.data;
        }
    });
    return adminer;
}


/**
 * @returns {Promise<Array | undefined>}
 */
export async function getAllTableNameList()
{
    var list;
    var url = getInterfaceURL("/db/getAllTableNameList");
    await getMethod(url)
    .then(function(result) {
        list = result.data;
    });
    return list;
}


/**
 * @returns {Promise<Array | undefined>}
 */
export async function getAllTableColumnNameList(tableName)
{
    var list;
    var url = getInterfaceURL("/db/getAllTableColumnNameList?tableName=" + tableName);
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            list = result.data;
    });
    return list;
}


/**
 * @param {string} tableName 
 * @param {string} columnName
 * @returns {Promise< object | undefined >} {CONSTRAINT_NAME:"",REFERENCED_TABLE_NAME:"",REFERENCED_COLUMN_NAME:""}
 */
export async function getTableColumnKeyUsage(tableName, columnName)
{
    var usage;
    const url = getInterfaceURL("/db/getColumnKeyUsage?tableName=" + tableName + "&columnName=" + columnName);
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            usage = result.data;
    });
    return usage;
}



/**
 * 
 * @param {string} tableName 
 * @param {string} columnName 
 */
export async function getTableColumnIsNullable(tableName, columnName)
{
    var is;
    const url = getInterfaceURL("/db/getColumnIsNullable?tableName=" + tableName + "&columnName=" + columnName);
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            is = result.data;
    });
    return is;
}


/**
 * 
 * @param {string} tableName 
 * @param {string} columnName 
 */
export async function getTableColumnDefaultValue(tableName, columnName)
{
    var defaultValue;
    var url = getInterfaceURL("/db/getColumnDefaultValue?tableName=" + tableName + "&columnName=" + columnName);
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            defaultValue = result.data;
    });
    return defaultValue;
}



/**
 * 
 * @param {string} tableName 
 * @param {string} columnName 
 * @returns 
 */
export async function getTableColumnDataType(tableName, columnName)
{
    var type;
    var url = getInterfaceURL("/db/getColumnDataType?tableName=" + tableName + "&columnName=" + columnName);
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            type = result.data;
    });
    return type;
}



/**
 * @param {string} tableName 
 * @param {number} pageIndex 
 * @returns {Promise<Array | undefined>}
 */
export async function getTablePage(tableName, pageIndex)
{
    var list;
    var url = getInterfaceURL("/db/getPage?tableName=" + tableName + "&pageIndex=" + pageIndex.toString());
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            list = result.data;
    });
    return list;
}


/**
 * 
 */
export async function getTablePageSize()
{
    var size = 0;
    var url = getInterfaceURL("/db/getPageSize");
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            size = result.data;
    });
    return size;
}


/**
 * 
 * @param {string} tableName 
 * @returns 
 */
export async function getTableTotalCount(tableName)
{
    var count = 0;
    var url = getInterfaceURL("/db/getTableTotalCount?tableName=" + tableName);
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            count = result.data;
    });
    return count;
}


/**
 * 
 * @param {string} tableName 
 * @param {string} columnName 
 * @param {*} value 
 */
export async function searchTableByColumnValue(tableName, columnName, value)
{
    var list;
    var url = getInterfaceURL("/db/searchByColumnValue?tableName=" + tableName + "&columnName=" + columnName + "&value=" + value);
    await getMethod(url)
    .then(function(result) {
        if (result.type ===  "Success")
            list = result.data;
    });
    return list;
}


/**
 * 
 * @param {string} tableName 
 * @param {object} obj 
 */
export async function dbInsert(tableName, obj)
{
    var type = "Failed";
    var url = getInterfaceURL("/db/insert");
    await postMethod(url, {
        tableName: tableName,
        map: obj
    }).then(function(result) {
        type = result.type;
        alert(result.message);
    });
    return type;
}


/**
 * 
 * @param {string} tableName 
 * @param {number} id 
 * @param {object} obj 
 */
export async function dbUpdateByID(tableName, id, obj)
{
    var type = "Failed";
    var url = getInterfaceURL("/db/updateByID");
    await postMethod(url, {
        tableName: tableName,
        id: id,
        map: obj
    }).then(function(result) {
        type = result.type;
        alert(result.message);
    });
    return type;
}


/**
 * 
 * @param {string} tableName 
 * @param {number} id 
 */
export async function dbDeleteByID(tableName, id)
{
    var type = "Failed";
    var url = getInterfaceURL("/db/deleteByID");
    await postMethodForm(url, {
        tableName: tableName,
        id: id
    }).then(function(result) {
        type = result.type;
        alert(result.message);
    });
    return type;
}



/**
 * @param {string} username 用户名
 * @param {string} password 密码
 * @returns {Promise<string | undefined>} result 为 token
*/
export async function login(username, password)
{
    if (username == null || username.length == 0)
    {
        alert("用户名不能为空");
        return;
    }
    if (password == null || password.length == 0)
    {
        alert("密码不能为空");
        return;
    }

    var token = "";

    var login_url = getInterfaceURL("/user/login");
    var data = {
        username: username,
        password: password
    }
    await postMethod(login_url, data).then(function(result) {
        alert(result.message);
        if (result.type ===  "Success")
        {
            axios.defaults.headers.common['token'] = result.data;
            token = result.data;
        }
    });
    return token;
}



/**
 * 需要先登录
 * @returns {Promise<object | undefined>} result 为 用户json对象
 */
export async function getUserSelf()
{
    var user;
    var url = getInterfaceURL("/user/getSelf");
    await getMethod(url).then(function(result) {
        if (result.type ===  "Success")
        {
            user = result.data;
        }
    });
    return user;
}



/**
 * @param {number} id 用户ID
 * @returns {Promise<object | undefined>} result 为 用户json对象
 */
export async function getUserByID(id)
{
    var user;

    var url = getInterfaceURL("/user/getUserByID?id=" + id.toString());
    await getMethod(url).then(function(result) {
        if (result.type ===  "Success")
        {
            user = result.data;
        }
    });
    return user;
}



/**
 * 使用 AvatarID 获取 base64编码的头像
 * @param {number} id
 * @returns {Promise<string | undefined>} result 为 base64编码的头像
 */
export async function getAvatarContentByID(id)
{
    var avatarContent;
    var url = getInterfaceURL("/avatar/getAvatarContentByID?id=" + id.toString());

    await getMethod(url)
	.then(function(result) {
        if (result.type === "Success")
        {
            avatarContent = result.data;
        }
    });
    return avatarContent;
}



/**
 * 使用 ownerID 获取群聊列表
 * @param {number} id 
 * @returns {Promise<Array<object> | undefined>} result 为 群聊对象数组
 */
export async function getGroupListByOwnerID(id)
{
    var list;
    var url = getInterfaceURL("/group/getGroupListByOwnerID?id=" + id.toString());
    await getMethod(url).then(function(result) {
        if (result.type === "Success")
        {
            list = result.data;
        }
    });
    return list;
}


/**
 * @param {number} id 群聊ID
 * @returns {Promise<boolean>} true 为 开启，false 为 关闭
 */
export async function getLiveStreamState(id)
{
    var state = false;
    var url = getInterfaceURL("/im/live/group/getLiveStreamState?id=" + id.toString());
    await getMethod(url)
	.then(function(result) {
        if (result.type === "Success")
        {
            if (result.data === "Living")
                state = true;
            else
                state = false;
        }
    });
    return state;
}


/**
 * @param id {number} 群聊ID
 * @param name {string} 直播间名称
 * @returns {Promise<boolean>} 为 true 表示成功，为 false 表示失败
 * */
export async function openGroupLiveStream(id, name)
{
	var trueOrFalse = false;
	var url = getInterfaceURL("/im/live/group/openLiveStream");
	await postMethod(url, {
		groupId: id,
		name: name
	}).then(function(result) {
		if (result.type === "Success")
		{
		    trueOrFalse = true;
		}
	});
	return trueOrFalse;
}


/**
 * @param id {number} 群聊ID
 * @returns {Promise<boolean>} 为 true 表示成功，为 false 表示失败
 * */
export async function closeGroupLiveStream(id)
{
	var trueOrFalse = false;
	var url = getInterfaceURL("/im/live/group/closeLiveStream");
	await postMethodForm(url, {
		id: id
	}).then(function(result) {
		if (result.type === "Success")
		{
		    trueOrFalse = true;
		}
	});
	return trueOrFalse;
}


/**
 * @param id {number} 群聊ID
 * */
export async function getAllADSTaskByGroupID(id)
{
	var list = null;
	var url = getInterfaceURL("/im/live/group/getAllLiveStreamVideoTimeCache?id=" + id.toString());
	await getMethod(url)
	.then(function(result) {
		list = result.data;
	});
	return list;
}


/**
 * @param groupId {number}
 * @param comment {string}
 * @param videoURL {string}
 * @param startTime {string}
 * @param endTime {string}
 */
export async function addLiveStreamVideoTimeCache(
	groupId,
	comment,
	videoURL,
	startTime,
	endTime
	)
{
	var type = null;
	var url = getInterfaceURL("/im/live/group/addLiveStreamVideoTimeCache");
	await postMethod(url, {
		groupId: groupId,
		comment: comment,
		videoURL: videoURL,
		startTime: startTime,
		endTime: endTime
	}).then(function(result) {
		alert(result.message);
		type = result.type;
	});
	return type;
}


/**
 * @param id {number} 任务缓存ID
 * */
export async function deleteLiveStreamVideoTimeCache(id)
{
	var url = getInterfaceURL("/im/live/group/deleteLiveStreamVideoTimeCache");
	await postMethodForm(url, {
		id: id
	}).then(function(result) {
		alert(result.message);
	});
}


